<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <el-form ref="searchMap" class="serach_wrapper flex-space-between" :inline="true" :model="searchMap" @submit.native.prevent>
        <div class="search">
          <el-form-item prop="keyword" size="small">
            <el-input v-model="searchMap.keyword" placeholder="请输入考试名称/试卷名称" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit flex">
          <!-- <el-button    
              icon="el-icon-plus"
              class="btn_primary"
              @click="handleAdd()">
              批量导入
          </el-button> -->
          <el-button icon="el-icon-plus" class="btn_primary" @click="handleAdd()">
            新增
          </el-button>
        </div>
      </el-form>
      <div class="table_container">
        <el-table stripe v-loading="examLoading" :data="examList">
          <el-table-column type="index" label="序号" header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="examName" label="考试名称" align="center"></el-table-column>
          <el-table-column prop="examTarget" label="考试对象" align="center"></el-table-column>
          <el-table-column prop="examRange" label="考试范围" align="center"></el-table-column>
          <el-table-column prop="examStartTime" label="考试日期时间" align="center"></el-table-column>
          <el-table-column prop="examPassScore" label="通过分数" align="center"></el-table-column>
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <span type="text" class="tag_primary" size="small" @click="handleDetails(scope.row)">编辑</span>
              <span type="text" class="tag_success" size="small" @click="handleInfo(scope.row)">结果查看</span>
              <span type="text" class="tag_primary" size="small" @click="selectLink(scope.row)">查看链接</span>
              <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination v-if="total>0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getExamList" />
    </div>
    <ExamDialog ref="examDialog" @replace="replace"></ExamDialog>
    <ExamStatDialog ref="examStatDialog"></ExamStatDialog>
  </div>
</template>

<script>
import { listExam, getExamById, deleteExam, getExamLink } from '@/api/examination'
import UploadDrag from '@/components/UploadFiles'
import ExamDialog from './ExamDialog'
import ExamStatDialog from './ExamStatDialog'
export default {
  dicts: ['exam_question_type'],
  data () {
    return {
      // 搜索数据
      searchMap: {
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        keyword: undefined,
        questionType: undefined
      },
      // 考试资料数据
      formData: {},
      // 访客预约表单验证
      examQuestionRules: {
        examMaterialName: [{ required: true, message: '请输入材料名称', trigger: 'blur' }],
      },
      // 考试资料数据加载状态
      examLoading: false,
      // 条数
      total: 0,
      // 考试资料数据弹框状态
      dialogVisableExamQuestion: false,
      // 标题
      title: '',
      // 考试数据列表数据
      examList: []
    };
  },
  created () {
    this.getExamList();
  },
  computed: {
    pageTitle () {
      return '考试管理'
    }
  },
  watch: {},
  methods: {
    getExamList () {
      this.examLoading = true;
      listExam(this.searchMap).then(res => {
        if (200 === res.code) {
          this.examList = res.rows
          this.total = res.total
          this.examLoading = false
        }
      })
    },
    // 点击资料详情
    handleDetails (row) {
      const examId = row.examId
      console.log(examId)
      getExamById(examId).then(res => {
        this.$refs.examDialog.show(res.data)
      })
    },
    // 点击删除访客预约
    handleDelete (row) {
      const examId = [row.examId]
      this.$confirm('是否删除该试卷').then(res => {
        return deleteExam(examId)
      }).then(() => {
        this.getExamList()
        this.$message.success('删除成功')
      })
    },
    // 点击新增考试资料
    handleAdd () {
      this.$nextTick(() => {
        this.$refs.examDialog.show()
      })
    },
    handleSearch () {
      this.getExamList()
    },
    // 重新加载页面
    replace () {
      this.getExamList()
    },
    async selectLink (row) {
      const response = await getExamLink(row.examId)
      if (response.code === 200 && response.data && response.data.url) {
        const urlToCopy = response.data.url
        this.$confirm(`考试链接:${response.data.url}`, '链接', {
          confirmButtonText: '点击复制',
          cancelButtonText: '取消',
          type: 'success'
        }).then(async () => {
          await navigator.clipboard.writeText(urlToCopy);
          this.$message({
            type: 'success',
            message: '复制成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      }
    },
    /**  考试信息 */
    handleInfo (row) {
      const examId = row.examId
      console.log(examId)
      getExamById(examId).then(res => {
        this.$refs.examStatDialog.show(res.data)
      })
    },
    handleReset() {
      this.searchMap = {
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        keyword: undefined,
        questionType: undefined
      }
      this.getExamList()
    }
  },
  components: {
    UploadDrag,
    ExamDialog,
    ExamStatDialog
  }
}
</script>
<style lang='less' scoped>
</style>